from rest_framework import serializers
from apps.api.project.models import ProjectModel
from apps.api.interface.serializers import InterfaceAndTestCaseNameModelSerializer, InterfaceAndConfigNameModelSerializer
from utils.common import date_format


class ProjectModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = ProjectModel
        fields = ["id", "name", "leader", "developer", "tester", "description", "create_time"]
        extra_kwargs = {
            "name": {
                "min_length": 2,
                "max_length": 20,
                "error_messages": {
                    "min_length": "项目名称长度为 2-20 个字符！",
                    "max_length": "项目名称长度为 2-20 个字符！"
                }
            },
            "leader": {
                "min_length": 2,
                "max_length": 20,
                "error_messages": {
                    "min_length": "项目领导名称长度为 2-20 个字符！",
                    "max_length": "项目领导名称长度为 2-20 个字符！"
                }
            },
            "create_time": {
                "read_only": True,
                "format": date_format()
            }
        }


class GetTestCaseNameModelSerializer(serializers.ModelSerializer):

    interface = InterfaceAndTestCaseNameModelSerializer(label="项目接口", help_text="项目接口", many=True, read_only=True)

    class Meta:
        model = ProjectModel
        fields = ["id", "name", "interface"]


class GetConfigNameModelSerializer(serializers.ModelSerializer):

    interface = InterfaceAndConfigNameModelSerializer(label="项目接口", help_text="项目接口", many=True, read_only=True)

    class Meta:
        model = ProjectModel
        fields = ["id", "name", "interface"]


class RunProjectModelSerializer(serializers.ModelSerializer):
    env_id = serializers.IntegerField(label="运行环境id", help_text="运行环境id", write_only=True)

    class Meta:
        model = ProjectModel
        fields = ["env_id"]
